package collect.taobao.util.generate;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;


import collect.taobao.util.Constant;

public class MVCFactory {
	
	
	public static void main(String[] args) {
		MVCFactory factory = new MVCFactory();
		factory.create("ClassificationDirectoryRelation");
	}
	
	public static String create(String name){
		try{
			generateBean(name);
			generateAction(name);
			generateSverive(name);
			generateDao(name);
			ormapping(name);
			setSpringConfig(name);
		}catch(Exception e){
			e.printStackTrace();
			return "generate MVC fail";
		}
		return "generate MVC success ";
		
	}
	
	public static void generateAction(String ActionName) throws IOException{
	
		String line = null;
		FileReader fr = null;
		FileWriter fw = null;
		try{
			String path = System.getProperty("user.dir").replace("\\", "/")+"/src/collect/taobao/util/generate/ControlAction.java"; 
			String target = System.getProperty("user.dir").replace("\\", "/")+"/src/collect/taobao/action/"+ActionName+"Action.java"; 
			File file = new File(path);
			fr = new FileReader(file);
			fw = new FileWriter(target);
			while ((line = fr.getNextLine()) != null) {
				int index = 0;
				if((index = line.indexOf("package"))>=0) {
					line.substring(index+8, line.length());
					line = "package" + " collect.taobao.action;";
					System.out.println(line);
				}
				if((index = line.indexOf(Constant.Cotrol))>=0) {
					line = line.replace(Constant.Cotrol,ActionName);
				}
				System.out.println(line);
				fw.write(line);
				fw.write("\r\n");
				fw.flush();
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(fr!=null)fr.close();
			if(fw!=null)fw.close();
		}
	}
	public static void generateSverive(String serviceName) throws IOException{
		String line = null;
		FileReader fr = null;
		FileReader implFr = null;
		FileWriter fw = null;
		FileWriter implFw = null;
		try{
			String path = System.getProperty("user.dir").replace("\\", "/")+"/src/collect/taobao/util/generate/LogicService.java"; 
			String target = System.getProperty("user.dir").replace("\\", "/")+"/src/collect/taobao/service/"+serviceName+"Service.java"; 
			String implPath = System.getProperty("user.dir").replace("\\", "/")+"/src/collect/taobao/util/generate/LogicServiceImpl.java"; 
			String implTarget = System.getProperty("user.dir").replace("\\", "/")+"/src/collect/taobao/service/impl/"+serviceName+"ServiceImpl.java"; 
			System.out.println(target);
			File file = new File(path);
			fr = new FileReader(file);
			File implFile = new File(implPath);
			implFr = new FileReader(implFile);
			fw = new FileWriter(target);
			implFw = new FileWriter(implTarget);
			while ((line = fr.getNextLine()) != null) {
				int index = 0;
				if((index = line.indexOf(Constant.packageKey))>=0) {
					line = Constant.packageKey + " collect.taobao.service;"+"\r\n";
					line = line + Constant.importKey + " collect.taobao.bean.EntityBean;"+"\r\n";
				}
				if((index = line.indexOf(Constant.Logic))>=0) {
					line = line.replace(Constant.Logic,serviceName);
				}
				if((index = line.indexOf(Constant.Entity))>=0) {
					line = line.replace(Constant.Entity,serviceName);
				}
				if((index = line.indexOf(Constant.entity))>=0) {
					line = line.replace(Constant.entity,serviceName.toLowerCase().substring(0,1)+serviceName.substring(1,serviceName.length()));
				}
				System.out.println(line);
				fw.write(line);
				fw.write("\r\n");
				fw.flush();
				
			}
			while ((line = implFr.getNextLine()) != null) {
				int index = 0;
				if((index = line.indexOf(Constant.packageKey))>=0) {
					line = Constant.packageKey + " collect.taobao.service.impl;"+"\r\n";
					line = line + Constant.importKey + " collect.taobao.service.LogicService;"+"\r\n";
					line = line + Constant.importKey + " collect.taobao.bean.EntityBean;"+"\r\n";
					line = line + Constant.importKey + " collect.taobao.dao.FreezeDao;"+"\r\n";
				}
				if((index = line.indexOf(Constant.Logic))>=0) {
					line = line.replace(Constant.Logic,serviceName);
				}
				if((index = line.indexOf(Constant.Freeze))>=0) {
					line = line.replace(Constant.Freeze,serviceName);
				}
				if((index = line.indexOf(Constant.freeze))>=0) {
					line = line.replace(Constant.freeze,serviceName.toLowerCase().substring(0,1)+serviceName.substring(1,serviceName.length()));
				}
				if((index = line.indexOf(Constant.Entity))>=0) {
					line = line.replace(Constant.Entity,serviceName);
				}
				if((index = line.indexOf(Constant.entity))>=0) {
					line = line.replace(Constant.entity,serviceName.toLowerCase().substring(0,1)+serviceName.substring(1,serviceName.length()));
				}
				System.out.println(line);
				implFw.write(line);
				implFw.write("\r\n");
				implFw.flush();
			}
		}catch(Exception e){
				e.printStackTrace();
			}finally{
				if(fr!=null)fr.close();
				if(fw!=null)fw.close();
				if(implFr!=null)fr.close();
				if(implFw!=null)fw.close();
			}
	}
	public static void generateDao(String daoName) throws IOException{
		String line = null;
		FileReader fr = null;
		FileReader implFr = null;
		FileWriter fw = null;
		FileWriter implFw = null;
		try{
			String path = System.getProperty("user.dir").replace("\\", "/")+"/src/collect/taobao/util/generate/FreezeDao.java"; 
			String target = System.getProperty("user.dir").replace("\\", "/")+"/src/collect/taobao/dao/"+daoName+"Dao.java"; 
			String implPath = System.getProperty("user.dir").replace("\\", "/")+"/src/collect/taobao/util/generate/FreezeDaoImpl.java"; 
			String implTarget = System.getProperty("user.dir").replace("\\", "/")+"/src/collect/taobao/dao/impl/"+daoName+"DaoImpl.java"; 
			System.out.println(target);
			File file = new File(path);
			fr = new FileReader(file);
			File implFile = new File(implPath);
			implFr = new FileReader(implFile);
			fw = new FileWriter(target);
			implFw = new FileWriter(implTarget);
			while ((line = fr.getNextLine()) != null) {
				int index = 0;
				if((index = line.indexOf(Constant.packageKey))>=0) {
					line.substring(index+8, line.length());
					line = Constant.packageKey + " collect.taobao.dao;"+"\r\n";
					line = line + Constant.importKey + " collect.taobao.bean.EntityBean;"+"\r\n";
				}
				if((index = line.indexOf(Constant.Freeze))>=0) {
					line = line.replace(Constant.Freeze,daoName);
				}
				if((index = line.indexOf(Constant.Entity))>=0) {
					line = line.replace(Constant.Entity,daoName);
				}
				if((index = line.indexOf(Constant.entity))>=0) {
					line = line.replace(Constant.entity,daoName.toLowerCase().substring(0,1)+daoName.substring(1,daoName.length()));
				}
				
				System.out.println(line);
				fw.write(line);
				fw.write("\r\n");
				fw.flush();
				
			}
			while ((line = implFr.getNextLine()) != null) {
				int index = 0;
				if((index = line.indexOf(Constant.packageKey))>=0) {
					line.substring(index+Constant.eitht, line.length());
					line = Constant.packageKey + " collect.taobao.dao.impl;"+"\r\n";
					line = line + Constant.importKey + " collect.taobao.dao.FreezeDao;"+"\r\n";
					line = line + Constant.importKey + " collect.taobao.bean.EntityBean;"+"\r\n";
				}
				if((index = line.indexOf(Constant.Freeze))>=0) {
					line = line.replace(Constant.Freeze,daoName);
				}
				if((index = line.indexOf(Constant.freeze))>=0) {
					line = line.replace(Constant.freeze,daoName.toLowerCase().substring(0,1)+daoName.substring(1,daoName.length()));
				}
				if((index = line.indexOf(Constant.Entity))>=0) {
					line = line.replace(Constant.Entity,daoName);
				}
				if((index = line.indexOf(Constant.entity))>=0) {
					line = line.replace(Constant.entity,daoName.toLowerCase().substring(0,1)+daoName.substring(1,daoName.length()));
				}
				System.out.println(line);
				implFw.write(line);
				implFw.write("\r\n");
				implFw.flush();
			}
		}catch(Exception e){
				e.printStackTrace();
			}finally{
				if(fr!=null)fr.close();
				if(fw!=null)fw.close();
				if(implFr!=null)fr.close();
				if(implFw!=null)fw.close();
			}
	}
	
	public static void ormapping(String xmlName) throws IOException{
		
		String line = null;
		FileReader fr = null;
		FileWriter fw = null;
		try{
			String path = System.getProperty("user.dir").replace("\\", "/")+"/src/collect/taobao/util/generate/ORM.hbm.xml"; 
			String target = System.getProperty("user.dir").replace("\\", "/")+"/src/collect/taobao/bean/"+xmlName+"Bean.hbm.xml"; 
			File file = new File(path);
			File targetFile = new File(target);
			fr = new FileReader(file);
			fw = new FileWriter(targetFile);
			while ((line = fr.getNextLine()) != null) {
				int index = 0;
				if((index = line.indexOf(Constant.ORM))>=0) {
					line = line.replace(Constant.ORM,xmlName);
				}
				System.out.println(line);
				fw.write(line);
				fw.write("\r\n");
				fw.flush();
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(fr!=null)fr.close();
			if(fw!=null)fw.close();
		}
	}
	
	public static void setSpringConfig(String beanName) throws IOException{
		
		StringBuffer strBuf = new StringBuffer();;
		String line = "";
		FileReader fr = null;
		FileWriter fw = null;
		try{
			String targetPath = System.getProperty("user.dir").replace("\\", "/")+"/src/spring-config/application-set.xml"; 
			File targetFile = new File(targetPath);
			fr = new FileReader(targetFile);
		
			int index = 0;
				System.out.println(targetPath);
			while ((line = fr.getNextLine()) != null) {
//				strBuf.append(line).append("\r\n");
				if(!((index = line.indexOf("</beans>"))>=0)){
					strBuf.append(line+"\r\n");
				}else if((index = line.indexOf("</beans>"))>=0) {
					line ="";
					strBuf.append("\t"+"<bean id=\""+beanName.toLowerCase().substring(0,1)+beanName.substring(1,beanName.length())+"ServerInterface\" "+"\r\n");
					strBuf.append("\t\t"+"class=\"collect.taobao.service.impl."+beanName+"ServiceImpl\">" +"\r\n");
					strBuf.append("\t\t"+"<property name=\""+beanName.toLowerCase().substring(0,1)+beanName.substring(1,beanName.length())+"Dao\">"+"\r\n");
					strBuf.append("\t\t\t"+"<ref bean=\""+beanName.toLowerCase().substring(0,1)+beanName.substring(1,beanName.length())+"DaoInterface\" />"+"\r\n");
					strBuf.append("\t\t"+"</property>"+"\r\n");
					strBuf.append("\t"+"</bean>"+"\r\n");
					strBuf.append("\t\t"+"<bean id=\""+beanName.toLowerCase().substring(0,1)+beanName.substring(1,beanName.length())+"DaoInterface\""+"\r\n");
					strBuf.append("\t\t"+"class=\"collect.taobao.dao.impl."+beanName+"DaoImpl\">"+"\r\n");
					strBuf.append("\t\t"+"<property name=\"sessionFactory\" ref=\"sessionFactory\"></property>"+"\r\n");
					strBuf.append("\t"+"</bean>");
					strBuf.append("\r\n").append("\r\n");
					strBuf.append("</beans>" +"\r\n");
				}
			}
			fw = new FileWriter(targetFile);
			fw.write(strBuf.toString());
			fw.flush();
			System.out.println(strBuf.toString());
	
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(fr!=null)fr.close();
			if(fw!=null)fw.close();
		}
	}
	
	public static void generateBean(String beanName) throws IOException{
		StringBuffer strBuf = new StringBuffer();
		FileReader fr = null;
		FileWriter fw = null;
		try{
			String path = System.getProperty("user.dir").replace("\\", "/")+"/src/collect/taobao/bean/"+beanName+"Bean.java"; 
			File file = new File(path);
			fw = new FileWriter(file);
			if(!file.exists()){
				file.createNewFile();
				strBuf.append("package collect.taobao.bean;"+"\r\n");
				strBuf.append("public class "+beanName+"Bean {"+"\r\n");
				strBuf.append("}"+"\r\n");
			}else{
				strBuf.append("package collect.taobao.bean;"+"\r\n");
				strBuf.append("public class "+beanName+"Bean {"+"\r\n");
				strBuf.append("}"+"\r\n");
			}
			fw.write(strBuf.toString());
//			System.out.println(strBuf);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(fr!=null)fr.close();
			if(fw!=null)fw.close();
		}
	}
	
	

}
